package com.stk.doublebreak;

import java.util.Random;

import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.shape.IShape;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.graphics.Point;
import android.util.Log;

public class Asteroid{
	
	private Segment shape[];
	private Segment shadow[];
	private int side;
	private int screenWidth;
	private int screenHeight;
	private float speed;
	private Core centre;
	
	
	public Asteroid (int sW, int sH, VertexBufferObjectManager vbom, int variety, float speed){
		this.screenWidth = sW;
		this.screenHeight = sH;
		
		Random rand  =  new Random();
		this.side = this.screenWidth / 37;
		this.speed = speed + rand.nextInt(20);
		
		int sideNumber = rand.nextInt(variety);
		this.shape =  new Segment[sideNumber+3];
		this.shadow =  new Segment[sideNumber+3];
		this.buildShape(vbom);
	}

	private void buildShape(VertexBufferObjectManager vbom) {
		Random rand  =  new Random();
		
		Point startPos;		
		
		if (rand.nextBoolean()){
			startPos = new Point(rand.nextInt(screenWidth/2), - this.side * 2);
		}else{
			startPos = new Point(-this.side*2, rand.nextInt(screenHeight/2));
		}
		
		this.centre = new Core(startPos.x, startPos.y, vbom, this.speed, this.screenWidth, this.screenHeight);


		for (int i=0; i < this.shape.length; i++){
			
			shape[i] = new Segment((float)(side/2 * Math.cos(2*Math.PI*(i+1)/this.shape.length)) + startPos.x,
					(float)(side/2 * Math.sin(2*Math.PI*(i+1)/this.shape.length)) + startPos.y,
					(float)(side/2 * Math.cos(2*Math.PI*(i+2)/this.shape.length)) + startPos.x,
					(float)(side/2 * Math.sin(2*Math.PI*(i+2)/this.shape.length)) + startPos.y,
					3, vbom, this.speed, screenWidth, screenHeight);
			shadow[i] = new Segment((float)(side/2 * Math.cos(2*Math.PI*(i+1)/this.shape.length)) + startPos.x,
					(float)(side/2 * Math.sin(2*Math.PI*(i+1)/this.shape.length)) + startPos.y,
					(float)(side/2 * Math.cos(2*Math.PI*(i+2)/this.shape.length)) + startPos.x,
					(float)(side/2 * Math.sin(2*Math.PI*(i+2)/this.shape.length)) + startPos.y,
					8, vbom, this.speed, screenWidth, screenHeight);
			
			if (shape.length == 3){
				shadow[i].setColor(Util.HexToRGB("#CC0000"));
				shape[i].setColor(Util.HexToRGB("#FF4444"));
			}else if (shape.length == 4){
				shadow[i].setColor(Util.HexToRGB("#FF8800"));
				shape[i].setColor(Util.HexToRGB("#FFBB33"));
			}else if (shape.length == 5){
				shadow[i].setColor(Util.HexToRGB("#669900"));
				shape[i].setColor(Util.HexToRGB("#99CC00"));
			}else if (shape.length == 6){
				shadow[i].setColor(Util.HexToRGB("#9933CC"));
				shape[i].setColor(Util.HexToRGB("#AA66CC"));
			}	        
		}				
	}
	
	public boolean checkOut(){
		if (centre.isOut()){
			return true;
		}
		return false;
	}
	
	public boolean collidesWithPlayer(Player p){
		float ax = p.getX1();
		float ay = p.getY1();
		float bx = p.getX2();
		float by = p.getY2();
		float cx = this.centre.getX();
		float cy = this.centre.getY();
		double r_numerator = (cx-ax)*(bx-ax) + (cy-ay)*(by-ay);
		double r_denomenator = (bx-ax)*(bx-ax) + (by-ay)*(by-ay);
		double r = r_numerator / r_denomenator;
	    
	    double s =  ((ay-cy)*(bx-ax)-(ax-cx)*(by-ay) ) / r_denomenator;

		 double distanceLine = Math.abs(s)*Math.sqrt(r_denomenator);

		
		double distanceSegment = -1;

		if ( (r >= 0) && (r <= 1) )
		{
			 distanceSegment = distanceLine;
		}
		else
		{
			double dist1 = (cx-ax)*(cx-ax) + (cy-ay)*(cy-ay);
			double dist2 = (cx-bx)*(cx-bx) + (cy-by)*(cy-by);
			if (dist1 < dist2)
			{
				distanceSegment = Math.sqrt(dist1);
			}
			else
			{
				distanceSegment = Math.sqrt(dist2);
			}
		}
		
		if (distanceSegment == distanceLine && distanceSegment <= (side/3)*2){
			return true;
		}
		return false;
	}


	public Segment[] getShape() {
		return shape;
	}

	public Segment[] getShadow() {
		return shadow;
	}
	
	public Core getCore(){
		return centre;
	}
}
